package com.etc.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.Doctor;
import com.etc.entity.Patient;
import com.etc.service.DoctorService;
import com.etc.service.PatientService;

/**
 * 医生相关的操作 控制器
 * 
 * @author 小白
 *
 */
@Controller
public class DoctorController {
	@Autowired
	private DoctorService doctorService;
	@Autowired
	private PatientService patientService;

	/**
	 * ajax请求 查询单个医生对象
	 * 
	 * @param doctorid
	 * @return
	 */
	@RequestMapping(value = "doctor/{doctorid}", method = RequestMethod.GET)
	@ResponseBody
	public Doctor getDoctorById(@PathVariable(value = "doctorid") int doctorid) {
		return doctorService.selectByPrimaryKey(doctorid);

	}
	/**
	 * 查询所有医生对象，返回首页
	 * 
	 * @param doctorid
	 * @return
	 */
	@RequestMapping(value = "doctorList", method = RequestMethod.GET)
	public String getDoctor(HttpServletRequest request) {
		List<Doctor> doctorlist= doctorService.queryBusers();
		//把所有医生对象放在session里，方便其他页面用
		request.getSession().setAttribute("doctorlist", doctorlist);
		return "forward:/front/index.jsp";
	}
	/**
	 * 查询单个医生对象，返回你要的页面
	 * @param doctorid  医生Id
	 * @param model
	 * @param url 返回页面路径
	 * @return
	 */
	@RequestMapping(value = "doctor1/{doctorid}", method = RequestMethod.GET)
	public String getDoctorById1(@PathVariable(value = "doctorid") int doctorid, Model model,String url) {
		Doctor doctor = doctorService.selectByPrimaryKey(doctorid);
		model.addAttribute("doctor", doctor);
		return url;
	}
	/**
	 * 医生查询病人信息
	 * @param doctorid
	 * @return
	 */
	@RequestMapping(value="selPatient/{patienttel}", method = RequestMethod.GET)
	@ResponseBody
	public Patient getPatientByTel(@PathVariable(value="patienttel") String patienttel) {
		return patientService.selByTel(patienttel);
		
	}
	/**
	 * 查询所有医生 传到后台页面
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "doctorListBack", method = RequestMethod.GET)
	@ResponseBody
	public String getDoctorBack(HttpServletRequest request) {
		List<Doctor> doctorlistback= doctorService.queryBusers();
		//把所有医生对象放在session里，方便其他页面用
		request.getSession().setAttribute("doctorlistback", doctorlistback);
		return "forward:/back/dangan.jsp";
	}
	
	/**
	 * 添加医生到后台页面
	 * @param doctor
	 * @return
	 */
	@RequestMapping(value="doctorAdd",method=RequestMethod.POST)
	@ResponseBody
	public int addDoctor(@RequestBody Doctor doctor) {
		int i = doctorService.insDoctor(doctor);
		return i;
	}
	
	/**
	 * 后台修改医生信息
	 * @param doctor
	 * @return
	 */
	@RequestMapping(value="doctorUpd/{doctorid}",method=RequestMethod.PUT)
	@ResponseBody
	public int updDoctor(@PathVariable(value="doctorid") int doctorid,@RequestBody Doctor doctor) {
		int i = doctorService.updDoctor(doctor);
		return i;
	}
	
	/**
	 * 后台查询医生总人数
	 * @return
	 */
	@RequestMapping(value="doctorCount")
	@ResponseBody
	public int countDoctor() {
		int c = doctorService.countDoctor();
		return c;
	}
	
	/**
	 * 根据医生编号删除医生
	 * @return
	 */
	@RequestMapping(value="doctorDel/{doctorid}",method=RequestMethod.DELETE)
	@ResponseBody
	public int delDoctor(@PathVariable(value="doctorid") int doctorid) {
		return doctorService.delDoctor(doctorid);
		
		
	}
}
